//
//  KSUser.m
//  Kosun
//
//  Created by zhaoke.hzk on 2017/5/13.
//  Copyright © 2017年 刘松坡. All rights reserved.
//

#import "HKUser.h"
#import "AppDelegate.h"

static HKUser* global_user = nil;

HKUser* WMglobalUser(void) {
    if (global_user == NULL) {
        NSData* userData = [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
        NSDictionary* userDic;
        if (userData) {
          userDic   = [NSJSONSerialization JSONObjectWithData:userData options:NSJSONReadingMutableLeaves  error:nil];
        }
        
        global_user = [HKUser mj_objectWithKeyValues:userDic];
    }
    return global_user;
}

void  updateUser(HKUser* user){
    global_user = user;
    NSDictionary* dic = [user mj_keyValues];
    NSData* data;
    if (dic) {
      data  = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"userInfo"];
}

void logout(void) {
    updateUser(nil);
    [[AppDelegate appDelegate] initAppRootVC];
}


@implementation HKUser

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"userId" : @"id"};
}

@end

@implementation HKUserModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"modelId" : @"id"};
}

@end

@implementation HKsCoreRecordsModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"recordId" : @"id"};
}

@end

